package struts.actions;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.Manager;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

import struts.forms.ManagerForm;
import biz.manager.ManagerBiz;
import biz.manager.ManagerBizImpl;

public class ManagerAction extends MappingDispatchAction {
	private ManagerBiz magBiz = new ManagerBizImpl();

	public ActionForward getAllMagers(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		List<Manager> managers = (List<Manager>) magBiz.selAllManager();
		request.setAttribute("managers", managers);
		return mapping.findForward("getMags");
	}

	public ActionForward deleteMager(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Long magId = Long.parseLong(request.getParameter("magId"));
		magBiz.delManagerById(magId);
		return mapping.findForward("toGetMags");
	}

	public ActionForward modfiyReadyMager(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Long magId = Long.parseLong(request.getParameter("magId"));
		Manager manager = magBiz.selManagerById(magId);
		ManagerForm magForm = (ManagerForm) form;
		magForm.setManager(manager);
		return mapping.findForward("modfiyReadyMager");
	}

	public ActionForward modfiyMager(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		ManagerForm magForm = (ManagerForm) form;
		Manager manager = magForm.getManager();
		magBiz.updateManager(manager);
		return mapping.findForward("toGetMags");
	}

	public ActionForward addReadyMager(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		ManagerForm magForm = (ManagerForm) form;
		magForm.setManager(new Manager());
		return mapping.findForward("addReadyMager");
	}

	public ActionForward addMager(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		ManagerForm magForm = (ManagerForm) form;
		Manager manager = magForm.getManager();
		magBiz.addManager(manager);
		return mapping.findForward("toGetMags");
	}
}
